package com.bdy.lm.taximanager.http;

/**
 * Created by Administrator on 2016/9/13 0013.
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;

import com.bdy.lm.taximanager.R;
import com.bdy.lm.taximanager.app.MyApp;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.io.File;
import java.io.FileNotFoundException;

import org.apache.http.Header;


public class HttpImage {

    private static Bitmap receivedBitmap = null;
    private static String TAG = "HttpImage";

    public static Bitmap getReceivedBitmap() {

        return receivedBitmap;
    }

    /**
     * get方法获取网络图片
     * @param c
     * @param ICID 司机IC卡号
     */
    public static void httpImageGet(final Context c, String ICID) {
        ImageRequest imageRequest = new ImageRequest(
                MyApp.getDriverPhoto + ICID, //图片url
                new Response.Listener<Bitmap>() { //成功时的回调
                    public void onResponse(Bitmap response) {
                        receivedBitmap = response;
                        Log.d(TAG, "图片获取成功");
                    }
                }
                //第三第四个参数分别用于指定允许图片最大的宽度和高度，如果指定的网络图片的宽度或高度大于这里的最大值，则会对图片进行压缩
                //指定成0的话就表示不管图片有多大，都不会进行压缩
                , 0, 0,
                Bitmap.Config.RGB_565, new Response.ErrorListener() {
                    public void onErrorResponse(VolleyError e) {
                        Log.e(TAG, "接收失败，error ：" + e);
                        receivedBitmap = BitmapFactory.decodeResource(c.getResources(), R.drawable.driver_photo);
                    }
                }
        );
        Log.d(TAG, MyApp.getDriverPhoto + ICID);
        MyApp.requestQueue.add(imageRequest);
        MyApp.requestQueue.start();
    }

    /**
     *  图片上传
     * @param fileName 文件名
     * @param ICID 司机IC卡号
     */
    public static void httpImagePost(String fileName, String ICID) {
        File file = new File("/storage/sdcard1/出租车服务管理系统/监控图片/", fileName);
//        Log.d("HttpImagePost", file.getAbsolutePath());
//        Log.d("HttpImagePost", file.exists()+"");
        if (file.exists() && file.length() > 0) {
            AsyncHttpClient client = new AsyncHttpClient();
            RequestParams params = new RequestParams();
            try {
                params.put(fileName, file);
                params.put("method", "photoUpload");//加入终端和用户信息
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // 上传文件
            client.post(MyApp.getDriverPhoto + ICID, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    // 上传成功后要做的工作
                    //Toast.makeText(context, "上传成功", Toast.LENGTH_LONG).show();
                    //progress.setProgress(0);
                    Log.d("HttpImagePost", "图片上传成功");
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                    // 上传失败后要做到工作
                    Log.d("HttpImagePost", "图片上传失败");
                    //Toast.makeText(context, "上传失败", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onProgress(int bytesWritten, int totalSize) {
                    super.onProgress(bytesWritten, totalSize);
                    int count = (int) ((bytesWritten * 1.0 / totalSize) * 100);
                    // 上传进度显示
                    //progress.setProgress(count);
                    Log.e("上传 Progress>>>>>", bytesWritten + " / " + totalSize);
                }

                @Override
                public void onRetry(int retryNo) {
                    super.onRetry(retryNo);
                    // 返回重试次数
                }

            });
        } else {
            //Toast.makeText(mContext, "文件不存在", Toast.LENGTH_LONG).show();
            Log.d("HttpImagePost", "图片文件不存在");
        }
    }
}